package com.amin.app.web.controller;

import com.amin.app.web.entity.Page;
import com.amin.app.web.entity.SDict;
import com.amin.app.web.entity.SDictList;
import com.amin.app.web.service.DictListService;
import com.amin.app.web.service.DictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSessionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典列表控制器
 * 20210717
 */
@Controller
@RequestMapping(value = "/dictList")
public class DictListController {
    final
    DictListService dictListService;
    final
    DictService dictService;

    public DictListController(DictListService dictListService, DictService dictService) {
        this.dictListService = dictListService;
        this.dictService = dictService;
    }

    /**
     * 获取字典子集的列表
     * @param model
     * @param page
     * @param sDictList
     * @return
     */
    @RequestMapping(value = "/getDictList")
    public String getDictList(Model model, Page page, SDictList sDictList){
        List<SDictList> sys_switch_type = dictListService.getDictListByDictType("sys_switch_type");
        model.addAttribute("systemSwitchType",sys_switch_type);

        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SDictList> dictList = dictListService.getDictList(sDictList);
        PageInfo<SDictList> dictListPage = new PageInfo<>(dictList);
        SDict sDict = new SDict();
        List<SDict> dicts = dictService.getDictList(sDict);
        model.addAttribute("dicts",dicts);
        model.addAttribute("dictList",dictListPage);
        model.addAttribute("sDictList",sDictList);

        return "/amin/dict/dictChildren";
    }

    /**
     * 添加字典/编辑字典
     * @param model
     * @param sDict
     * @return
     */
    @RequestMapping(value = "/createDict")
    public String createDict(Model model,SDictList sDict){
        List<SDictList> sys_switch_type = dictListService.getDictListByDictType("sys_switch_type");
        model.addAttribute("systemSwitchType", sys_switch_type);
        List<SDictList> sys_yes_no = dictListService.getDictListByDictType("sys_yes_no");
        model.addAttribute("systemYseNo", sys_yes_no);
        sDict.setDictState("0");
        sDict.setSystemDefault("0");
        if (null != sDict.getId() && 0 != sDict.getId()){
            sDict = dictListService.getDictById(sDict.getId());
        }
        model.addAttribute("dict", sDict);
        return "/amin/dict/createDictList";
    }

    /**
     * 数据提交
     * @param sDictList
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dataSubmit")
    public Map<Object,Object> dataSubmit(SDictList sDictList){
        Map<Object,Object> answer = new HashMap<>();
        try {
            if (null == sDictList.getId() || 0 ==  sDictList.getId()){
                sDictList.setCreatedTime(new Date());
                dictListService.save(sDictList);
            }else {
                dictListService.update(sDictList);
            }
            answer.put("code",0);
            answer.put("msg","数据保存成功");
        }catch (SqlSessionException e){
            answer.put("code",1);
            answer.put("msg",e.getMessage());
        }
        return answer;
    }
    /**
     * 删除字典
     * @param model
     * @param sDictList
     * @return
     */
    @RequestMapping(value = "/delById")
    public String delById(Model model,SDictList sDictList){
        dictListService.delById(sDictList.getId());
        return "redirect:/dictList/getDictList";
    }

}
